-
Notifications
You must be signed in to change notification settings - Fork 187
DepositToAny slice for the destination #2469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
I changed the priority system between the different destinations to align with #2324 (review) |
IntegratedQuantum
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I immediately see problems with the allocation strategy, please fix them before I review the rest of the code.
src/Inventory.zig
Outdated
| if(self.dest.type == .creative) return; | ||
| if(self.dest.type == .crafting) return; | ||
| if(self.dest.type == .workbench) return; | ||
| defer if(self.owned) main.stackAllocator.free(self.destinations); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
command may run multiple times on the client side, freeing here causes a double free. Instead you can free this in finalize.
Also the stackAllocator is a poor choice, only use the stackAllocator for local allocations, this allocation persists for multiple threads and likely will be ran from a different thread as well.
Also the allocator shouldn't be hardcoded here, instead the caller should pass it.
Lastly who even owns the memory in the other case? How do you keep track of something that gets used possible many frames after passing it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
command may run multiple times on the client side, freeing here causes a double free. Instead you can free this in finalize. Also the stackAllocator is a poor choice, only use the stackAllocator for local allocations, this allocation persists for multiple threads and likely will be ran from a different thread as well.
done changed to the globalAllocator which is used for other payloads
Also the allocator shouldn't be hardcoded here, instead the caller should pass it.
so should I change the finalize to parse over the globalAllocator? That looks better to me at least.
Lastly who even owns the memory in the other case? How do you keep track of something that gets used possible many frames after passing it?
Yeah you are right there... I will look into how I want to solve that. I could maybe just dupe the slice so that its always owned? But I don't want to that in gui.zig where depositToAny is called... I will sleep over it and look tomorrow at the code again.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so should I change the finalize to parse over the globalAllocator? That looks better to me at least.
No, it should be passed on creation, since the finalize call is already really disconnected from the creation.
This would prevent accidentally using mismatched allocators.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
switched the owned variable with an allocator. Also did the other issue with an dupe so it should now always be owned.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also consider making a separate .init function that allocates everything inside. Then I would be fine with not having the allocator stored since it's all used locally.
src/Inventory.zig
Outdated
| if(self.dest.type == .creative) return; | ||
| if(self.dest.type == .crafting) return; | ||
| if(self.dest.type == .workbench) return; | ||
| if(self.destinations.len == 0) return; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When would this even happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A modified Client can send anything. I just want to put a check in before it later causes bugs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is about safety of client data, then you should put this check in the deserialization function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Client data should generally be sanitized during or immediately after receiving.
src/Inventory.zig
Outdated
| for(self.dest._items, 0..) |*destStack, destSlot| { | ||
| if(destStack.item == .null and selectedEmptySlot == null) { | ||
| selectedEmptySlot = @intCast(destSlot); | ||
| var selectedEmptyInv: u8 = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why limit this to a u8? It should be usize, there is nothing gained by using a u8, other than future bugs from the intCast.
src/Inventory.zig
Outdated
|
|
||
| fn serialize(self: DepositToAny, writer: *utils.BinaryWriter) void { | ||
| writer.writeEnum(InventoryId, self.dest.id); | ||
| writer.writeInt(u8, @intCast(self.destinations.len)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use a varint and send a usize
Putting an arbitrary limit on the number and crashing when exceeded is not helpful.
If you want to limit this then you should have a good reason for it, and 256 might definitely be in reach for a deposit to nearby chests function.
src/Inventory.zig
Outdated
| if(emptySlot != null and (selectedEmptySlot == null or (hasItem and !selectedEmptyInvHasItem))) { | ||
| selectedEmptySlot = emptySlot; | ||
| selectedEmptyInv = @intCast(destInv); | ||
| selectedEmptyInvHasItem = hasItem; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is different from the behavior suggested in #2324 (review), here you seem to be first topping off partially filled stacks in the other inventories.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all done
IntegratedQuantum
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some small things, otherwise it seems reasonable
|
rest of the requested changes are also done |
Splitted of from #2324
First of two Payloads that need change to accept slices as destinations for the split of the hotbar to work.
Even if the split of the hotbar is not accepted this could still be necessary. See comment: #2324 (review)
I don't like the allocation solution so if anybody has a better solution it would be very welcome.
The renaming to
destinationsis to usedestin the for loops and to show that this is different to the other payloadsdestparameter